<template>
	<view class="content">
		<view class="all">
			<!-- 搜索框 -->
			<view class="flexleft">
				<view class="ss flexleft">
					<image src="@/static/images/sy_slices/search.png" mode="" class="search"></image>
					<input type="text" placeholder="请输入搜索内容" class="inp" v-model="ss_content">
				</view>
				<view class="sea" @click="tzspss">搜索</view>
			</view>
			<!-- 历史 -->
			<view class="nav">
				<view class="flexbetween">
					<view class="history">历史搜索 </view>
					<view class="del" @click="empty" v-if="ss_ls.length"></view>
				</view>
				<!-- 历史记录内容 -->
				<view class=" flexleft flex_wrap">
					<view class="del_a" v-for="(item, index) in ss_ls" :key="index" @click="clickssls(item)">{{item}}
					</view>
				</view>
			</view>
			<!--  -->
		</view>
	</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				ss_ls: [],
				ss_content: ""
			}
		},
		async onLoad() {
			let list = await uni.getStorage({
				key: 'searchList'
			});
			if (list[1].data) {
				this.ss_ls = JSON.parse(list[1].data);
			}
		},
		methods: {
			clickssls(item) {
				this.ss_content = item
				this.tzspss()
			},
			// 跳转商品搜索
			tzspss() {
				let _this = this
				if (_this.ss_content == '') {
					httpRequest.toast('请输入输入内容');
					return false
				} else {
					if (!_this.ss_ls.includes(_this.ss_content)) {
						_this.ss_ls.unshift(_this.ss_content);
						uni.setStorage({
							key: 'searchList',
							data: JSON.stringify(_this.ss_ls)
						});
					} else {
						//有搜索记录，删除之前的旧记录，将新搜索值重新push到数组首位
						let i = _this.ss_ls.indexOf(_this.ss_content);
						_this.ss_ls.splice(i, 1);
						_this.ss_ls.unshift(_this.ss_content);
						uni.setStorage({
							key: 'searchList',
							data: JSON.stringify(_this.ss_ls)
						});
					}
				}
				uni.navigateTo({
					url: '/pages/index/sousuo-lb?text=' + _this.ss_content
				})
				_this.ss_content = ""
			},
			//清空历史记录
			empty() {
				httpRequest.modal('提示', '请再次确认', true, (res) => {
					if (res) {
						httpRequest.toast('清除成功');
						uni.removeStorage({
							key: 'searchList'
						});
						this.ss_ls = [];
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fafafa;
		min-height: 100vh;
	}

	.all {
		padding: 0rpx 24rpx;
	}

	.search {
		width: 27rpx;
		height: 27rpx;
	}

	.ss {
		width: 560rpx;
		background-color: #f3f3f3;
		border-radius: 60rpx 60rpx 60rpx 60rpx;
		padding: 18rpx 20rpx 18rpx 42rpx;
	}

	.inp {
		background-color: #f3f3f3;
		width: 400rpx;
		padding: 0rpx 0rpx 0rpx 24rpx;
	}

	input:input-placeholder {
		font-size: 24rpx;
	}

	.sea {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #228DFF;
		padding-left: 20rpx;
	}

	.nav {
		margin-top: 24rpx;

		.history {
			font-size: 24rpx;
		}
	}

	.del_a {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
		margin-top: 31rpx;
		padding: 9rpx 35rpx 10rpx 35rpx;
		background-color: #ffffff;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		margin-right: 30rpx;
	}
</style>